package com.chengxiaoxiao.libarypartner.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.chengxiaoxiao.libarypartner.R;
import com.chengxiaoxiao.libarypartner.constants.AppConsrants;
import com.chengxiaoxiao.libarypartner.utils.ApiAddress;
import com.chengxiaoxiao.libarypartner.utils.SPUtils;
import com.chengxiaoxiao.libarypartner.utils.ToastUtils;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.image.SmartImageView;

import org.apache.http.Header;
import org.json.JSONException;
import org.json.JSONObject;

public class ScanActivity extends Activity
{

    private SmartImageView imageBook;
    private TextView textBookInfo;
    private Button btnGoOn;
    private Button btnAdd;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_scan);
        imageBook = (SmartImageView) findViewById(R.id.imageBook);
        textBookInfo = (TextView) findViewById(R.id.textBookInfo);
        btnGoOn = (Button) findViewById(R.id.btnGoOn);
        btnAdd = (Button) findViewById(R.id.btnAdd);


        final Intent intent = getIntent();
        String code = intent.getStringExtra(AppConsrants.SCAN_MSG_BOOK_CODE);

        initBook(code);

        btnGoOn.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                //立即借书

                if(bookId>0)
                {
                    Intent intent1 = new Intent(ScanActivity.this, BorrowActivity.class);
                    intent1.putExtra(AppConsrants.SCAN_BORROW_IDS, bookId+"");
                    startActivity(intent1);
                }


            }
        });

        btnAdd.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                //添加到借书单

                if(bookId==0)
                {
                    return;
                }

                //添加到借书单
                String ids = SPUtils.getString(AppConsrants.BORROW_LIST_ID,"",ScanActivity.this);
                if(!ids.contains(bookId+""))
                {
                    SPUtils.putString(AppConsrants.BORROW_LIST_ID,ids+","+bookId,ScanActivity.this);
                }

                ToastUtils.makeText(ScanActivity.this,"书籍已成功添加到借书单");


            }
        });


    }



    int bookId = 0;

    private void initBook(String code)
    {
        final AsyncHttpClient client = new AsyncHttpClient();
        client.get(ApiAddress.getApiDomain() + "/client/ClientServlet?op=getBookByIsbn&isbn=" + code, new AsyncHttpResponseHandler()
        {
            @Override
            public void onSuccess(int statusCode, org.apache.http.Header[] headers, byte[] responseBody)
            {

                String body = new String(responseBody);
                try
                {
                    JSONObject jsonObj = new JSONObject(body);

                    int id = jsonObj.getInt("id");
                    bookId = id;
                    String author = jsonObj.getString("author");
                    String bookName = jsonObj.getString("name");
                    int number = jsonObj.getInt("number");
                    String categporyName = jsonObj.getString("categoryName");
                    String publisherName = jsonObj.getString("publisherName");
                    String imagesPath = jsonObj.getString("imagesSrc");
                    String address = jsonObj.getString("address");

                    //ToastUtils.makeText(ScanActivity.this,code);

                    imageBook.setImageUrl(imagesPath);
                    textBookInfo.setText("[" + bookName + "] " + author + " " + publisherName + " " + address);


                } catch (JSONException e)
                {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }


            }

            @Override
            public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error)
            {
                //LoadDialogUtils.hidden();
                ToastUtils.makeText(ScanActivity.this, "网络连接错误");
            }
        });

    }
}
